package com.example.hehecat.yuejian1.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.MineContract;
import com.example.hehecat.yuejian1.mvp.model.MineModel;
import com.example.hehecat.yuejian1.mvp.model.bean.UserBean;
import com.example.hehecat.yuejian1.ui.activity.LoginActivity;
import com.example.hehecat.yuejian1.ui.activity.UpdateMineActivity;
import com.example.hehecat.yuejian1.ui.wiget.MyStaticVariable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.Subscriber;

import static com.chad.library.adapter.base.listener.SimpleClickListener.TAG;

public class MineFragment extends Fragment implements MineContract.View {


    //    @BindView(R.id.foodinfo_return)
//    ImageView foodinfoReturn;
    @BindView(R.id.TextView)
    TextView TextView;

    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.my_activity)
    TextView myFollow;
    @BindView(R.id.my_club)
    TextView myCollect;
    @BindView(R.id.my_picture)
    TextView myPicture;
    @BindView(R.id.button1)
    Button button1;
    @BindView(R.id.button2)
    Button button2;

    Unbinder unbinder;


    int user_id;
    String nickname;
    @BindView(R.id.textView1)
    TextView textView1;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.textView5)
    TextView textView5;
    @BindView(R.id.textView6)
    TextView textView6;
    @BindView(R.id.my_master)
    TextView myMaster;
    @BindView(R.id.my_game)
    TextView myGame;
    @BindView(R.id.mine)
    LinearLayout mine;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);


        if (getArguments() != null) {
            user_id = getArguments().getInt("user_id");
        }
        user_id = MyStaticVariable.newInstance().getUserid();
        Log.d(TAG, "onCreateView:user_id" + user_id);


        MineModel model = new MineModel();
        model.mine(user_id, nickname, new Subscriber<UserBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(UserBean userBean) {
                textView2.setText(userBean.getNickname());
                textView4.setText(userBean.getAddress());
                textView6.setText(userBean.getPhone());
            }
        });
        return view;
    }


    public static MineFragment newInstance(String name) {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        args.putString("name", name);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.button1)
    void bt1OnClick() {
        startActivity(new Intent(getActivity(), UpdateMineActivity.class));
    }

    @OnClick(R.id.button2)
    void bt2OnClick() {
        startActivity(new Intent(getActivity(), LoginActivity.class));
    }


    @OnClick(R.id.my_club)
    void btClubCollect() {
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, new ClubCollectFragment(), null)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .addToBackStack(null)
                .commit();
    }

    @OnClick(R.id.my_activity)
    void btActivityCollect() {
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, new AcitvityCollectFragment(), null)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .addToBackStack(null)
                .commit();
    }

    @OnClick(R.id.my_picture)
    void btMyPicture() {
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, new MyPictureFragment(), null)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .addToBackStack(null)
                .commit();
    }

    @OnClick(R.id.my_master)
    void btMyMaster() {
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, new MasterCollectFragment(), null)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .addToBackStack(null)
                .commit();
    }

    @OnClick(R.id.my_game)
    void btMyGame() {
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, new GameCollectFragment(), null)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .addToBackStack(null)
                .commit();
    }
}
